package com.agileboot.domain.news.db;

import cn.hutool.core.collection.CollUtil;
import com.agileboot.common.core.page.PageDTO;
import com.agileboot.common.exception.ApiException;
import com.agileboot.common.exception.error.ErrorCode;
import com.agileboot.domain.news.command.NewsInfoCommand;
import com.agileboot.domain.news.query.NewsInfoQuery;
import com.agileboot.domain.news.vo.NewsInfoVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 新闻资讯表(NewsInfo)表服务实现类
 *
 * @author lx
 * @since 2023-12-29 18:00:21
 */
@Service
public class NewsInfoServiceImpl extends ServiceImpl<NewsInfoMapper, NewsInfo> implements NewsInfoService {


    @Override
    public void saveNewsInfo(NewsInfoCommand newsInfoCommand) {
        //校验参数
        checkFiled(newsInfoCommand);
        NewsInfo newsInfo = new NewsInfo();
        BeanUtils.copyProperties(newsInfoCommand, newsInfo);
        this.save(newsInfo);
    }

    @Override
    public void deleteNewsInfo(List<Long> ids) {
        //校验参数
        if (CollUtil.isEmpty(ids)) {
            throw new ApiException(ErrorCode.Business.COMMON_BULK_DELETE_IDS_IS_INVALID);
        }
        this.removeBatchByIds(ids);
    }

    @Override
    public void updateNewsInfo(NewsInfoCommand newsInfoCommand) {
        //校验参数
        checkFiled(newsInfoCommand);
        NewsInfo newsInfo = new NewsInfo();
        BeanUtils.copyProperties(newsInfoCommand, newsInfo);
        this.updateById(newsInfo);
    }

    @Override
    public PageDTO<NewsInfoVO> queryNewsInfo(NewsInfoQuery newsInfoQuery) {
        Page<NewsInfo> page = baseMapper.queryNewsInfo(newsInfoQuery.toPage(), newsInfoQuery.addQueryCondition());
        if (CollectionUtils.isNotEmpty(page.getRecords())) {
            List<NewsInfoVO> collect = page.getRecords().stream().map(newsInfo -> {
                NewsInfoVO newsInfoVO = new NewsInfoVO();
                BeanUtils.copyProperties(newsInfo, newsInfoVO);
                return newsInfoVO;
            }).collect(Collectors.toList());
            return new PageDTO<>(collect, page.getTotal());
        }
        return new PageDTO<>(Collections.emptyList(), page.getTotal());
    }

    @Override
    public NewsInfoVO getNewsInfoDetail(NewsInfoQuery newsInfoQuery) {
        Assert.isTrue(newsInfoQuery.getId() != null, "id不能为空");
        NewsInfo byId = this.getById(newsInfoQuery.getId());
        if (Objects.nonNull(byId)) {
            NewsInfoVO newsInfoVO = new NewsInfoVO();
            BeanUtils.copyProperties(byId, newsInfoVO);
            return newsInfoVO;
        }
        return null;
    }

    @Override
    public void editNewsInfoStatus(NewsInfoCommand newsInfoCommand) {
        checkUpdateStatus(newsInfoCommand);
        LambdaUpdateWrapper<NewsInfo> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(NewsInfo::getId,newsInfoCommand.getId());
        updateWrapper.set(NewsInfo::getStatus,newsInfoCommand.getStatus());
        this.update(updateWrapper);
    }

    @Override
    public List<NewsInfoVO> getNewsList() {
        LambdaQueryWrapper<NewsInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(NewsInfo::getCreateTime);
        wrapper.last("limit 3");
        List<NewsInfo> newsInfos = baseMapper.selectList(wrapper);
        List<NewsInfoVO> newsInfoVOS = new ArrayList<>();
        for (NewsInfo newsInfo : newsInfos) {
            NewsInfoVO newsInfoVO = new NewsInfoVO();
            BeanUtils.copyProperties(newsInfo,newsInfoVO);
            newsInfoVOS.add(newsInfoVO);
        }
        return newsInfoVOS;
    }

    private void checkUpdateStatus(NewsInfoCommand newsInfoCommand) {
        Assert.isTrue(Objects.nonNull(newsInfoCommand), "newsInfoCommand不能为空");
        Assert.isTrue(newsInfoCommand.getId()!=null, "id不能为空");
        Assert.isTrue(newsInfoCommand.getStatus()!=null, "status不能为空");
    }



    //校验newsInfoCommand对象
    private void checkFiled(NewsInfoCommand newsInfoCommand) {
        Assert.isTrue(Objects.nonNull(newsInfoCommand), "newsInfoCommand不能为空");
        Assert.isTrue(StringUtils.isNotBlank(newsInfoCommand.getNewsTitle()), "newsTitle不能为空");
        Assert.isTrue(newsInfoCommand.getNewsType() != null, "newsType不能为空");
        Assert.isTrue(StringUtils.isNotBlank(newsInfoCommand.getNewsUrl()), "newsUrl不能为空");
        Assert.isTrue(StringUtils.isNotBlank(newsInfoCommand.getNewsAbstract()), "newsAbstract不能为空");
    }


}

